package Work05;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TicktThread {
    public static class Tickets {
        private int number;

        public Tickets(Work01.Tickets ti) {
        }

        public Tickets(int number) {
            this.number = number;
        }

        //创建锁对象
        public final Lock lock = new ReentrantLock();

        public void ticket(int number) throws InterruptedException {//卖票窗口
            //卖票的人
            String name = Thread.currentThread().getName();
            try {
                lock.lock();//上锁
                //判断剩下的票够不够
                if (this.number >= number) {
                    System.out.println(name + "卖票" + number + "张");
                    this.number -= number;
                    System.out.println("票还剩" + this.number + "张");
                }
                if (this.number == 0) {
                    System.out.println("票已售罄");
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();//解锁
            }
        }

        public int getNumber() {
            return number;
        }

        public void setNumber(int number) {
            this.number = number;
        }
    }

}
